package com.wnxy.portal.config;

import com.wnxy.portal.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
// 开启配置项自动读取填充到对象属性中，且填充后的对象自动加入ioc容器
@EnableConfigurationProperties(UrlProperties.class)
public class MvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private UrlProperties urlProperties;

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> whiteList = urlProperties.getWhiteList();

        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns(whiteList)
                .addPathPatterns("/**"); // 拦截所有资源
    }


    /**
     * 配置/templates目录下的路径映射
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // addViewController 指定访问路径； setViewName指定对应/templates下的页面名称
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/cart.html").setViewName("cart");
        registry.addViewController("/register.html").setViewName("register");
        registry.addViewController("/calc.html").setViewName("calc");
        registry.addViewController("/order.html").setViewName("order");
    }
}
